'
游戏内暂停菜单
'

extends CanvasLayer

@onready var panel_container: PanelContainer = $MarginContainer/PanelContainer

var is_closing: bool = false#防止连点继续

#生成时用tween变大出现
func _ready() -> void:
	self.get_tree().paused = true
	$OptionsMeau.visible = false
	panel_container.pivot_offset = panel_container.size / 2
	$AnimationPlayer.play("default")
	%ResumeButton.pressed.connect(on_resume_pressed)
	%OptionsButton.pressed.connect(on_options_pressed)
	%QuitButton.pressed.connect(on_quit_pressed)
	var tween = self.create_tween()
	tween.tween_property(panel_container, "scale", Vector2.ZERO, 0)
	tween.tween_property(panel_container, "scale", Vector2.ONE, .3)\
	.set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK)
	pass

#处理未被处理的输入
func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("pause"):
		self.get_tree().root.set_input_as_handled()
		on_resume_pressed()
	pass

#继续游戏按钮，使用tween让菜单变小消失
func on_resume_pressed() -> void:
	if is_closing:
		return
	is_closing = true
	$AnimationPlayer.play_backwards("default")
	var tween = self.create_tween()
	tween.tween_property(panel_container, "scale", Vector2.ZERO, .3)\
	.set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_BACK)
	await tween.finished
	self.queue_free()
	self.get_tree().paused = false
	pass

#选择进入选择菜单，先调用更新函数
func on_options_pressed() -> void:
	self.hide()
	$OptionsMeau.update_display()
	$OptionsMeau.visible = true
	pass

#退出菜单，返回但主菜单
func on_quit_pressed() -> void:
	self.get_tree().paused = false
	ScreenTransition.change_screen("res://场景/ui/主菜单.tscn")
	pass
